/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.google.blockly.ohos.demo.unit;

import com.google.blockly.model.Field;
import com.google.blockly.model.FieldDate;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;


/**
 * Tests for {@link FieldDate}.
 */
public class FieldDateTest {
    private static final String INITIAL_VALUE = "2015-09-14";

    FieldDate mField;

    @Before
    public void setUp() {
        mField = new FieldDate("alphabet", INITIAL_VALUE);
    }

    @Test
    public void testConstructor() {
        Assert.assertEquals(Field.TYPE_DATE, mField.getType());
        Assert.assertEquals("alphabet", mField.getName());
        Assert.assertEquals(INITIAL_VALUE, mField.getSerializedValue());
    }

    @Test
    public void testSetDate() {
        Date date = new Date();
        mField.setDate(date);
        Assert.assertEquals(date, mField.getDate());

        date.setTime(date.getTime() + 86400000);
        mField.setTime(date.getTime());
        Assert.assertEquals(date, mField.getDate());

        Assert.assertTrue(mField.setFromString("2017-03-23"));
        Assert.assertEquals("2017-03-23", mField.getLocalizedDateString());
    }

    @Test
    public void testSetFromString() {
        Assert.assertFalse(mField.setFromString("today"));
        Assert.assertFalse(mField.setFromString("2017/03/03"));
        Assert.assertFalse(mField.setFromString(""));

    }

    @Test
    public void testClone() {
        FieldDate clone = mField.clone();

        Assert.assertNotSame(clone,mField);
        Assert.assertEquals(mField.getName(),clone.getName());
        Assert.assertNotSame(clone.getDate(), mField.getDate());
        Assert.assertEquals(mField.getDate(), clone.getDate());
    }

}
